package com.example.hospital.api.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.example.hospital.api.db.dao.DoctorPrescriptionDao;
import com.example.hospital.api.service.DoctorPrescriptionService;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Service
public class DoctorPrescriptionServiceImpl implements DoctorPrescriptionService {
    @Resource
    private DoctorPrescriptionDao doctorPrescriptionDao;

    @Resource
    private AmqpTemplate amqpTemplate;

    @Override
    public HashMap searchPrescriptionById(int id) {
        HashMap map = doctorPrescriptionDao.searchPrescriptionById(id);
        String rp = MapUtil.getStr(map, "rp");
        JSONArray array = JSONUtil.parseArray(rp);
        map.replace("rp",array);
        return map;
    }

    @Override
    public void contextLoads(Map param) {
        int patientCardId = MapUtil.getInt(param, "patientCardId");
        int subDeptId = MapUtil.getInt(param, "subDeptId");
        int doctorId = MapUtil.getInt(param, "doctorId");
        int registrationId = MapUtil.getInt(param, "id");
        String diagnosis = "流行性感冒";
        ArrayList rp = new ArrayList<>() {{
            add(new HashMap<>() {{
                put("name", "蓝岑颗粒");
                put("spec", "1袋/次");
                put("method", "每日三次；口服");
                put("num",3);
            }});
            add(new HashMap<>() {{
                put("name", "头孢拉定胶囊");
                put("spec", "1片/次");
                put("method", "每日两次；口服");
                put("num",2);
            }});
        }};
        HashMap map = new HashMap() {{
            put("uuid", IdUtil.simpleUUID().toUpperCase());
            //自己查询数据库中就诊卡ID
            put("patientCardId", patientCardId);
            put("diagnosis", diagnosis);
            put("subDeptId", subDeptId);
            put("doctorId", doctorId);
            //自己查询数据库中挂号单ID
            put("registrationId", registrationId);
            put("rp", rp);
        }};
        amqpTemplate.convertAndSend("prescription", JSONUtil.parseObj(map).toString());
    }

    @Override
    public int searchCount(int id) {
        int status=doctorPrescriptionDao.searchCount(id);
        if(status==0){
            return 1;
        }else {
            return 0;
        }
    }

    @Override
    public HashMap searchPrescriptionByRegistrationId(Map param) {
        HashMap map = doctorPrescriptionDao.searchPrescriptionByRegistrationId(param);
        String birthday = MapUtil.getStr(map, "patientBirthday");
        DateTime dateTime = new DateTime(birthday, "yyyy-MM-dd");
        int age = DateUtil.ageOfNow(dateTime);
        map.put("patientAge",age);
        map.remove("patientBirthday");
        String rp = MapUtil.getStr(map, "rp");
        JSONArray array = JSONUtil.parseArray(rp);
        map.replace("rp",array);
        return map;
    }
}
